package cn.codesea.housework_user.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.codesea.housework_user.R;
import cn.codesea.housework_user.adapter.listener.OnOperationClickListener;
import cn.codesea.housework_user.adapter.listener.OnRecyclerItemClickOrderListener;
import cn.codesea.housework_user.entity.Order;
import cn.codesea.housework_user.entity.Service;
import cn.codesea.housework_user.entity.User;
import cn.codesea.housework_user.entity.Worker;
import cn.codesea.housework_user.utils.DateUtil;

public class ManagerOrderRcvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context mContext;
    LayoutInflater mInflater;
    List<Order> orders;


    //声明自定义的监听接口
    private OnRecyclerItemClickOrderListener monItemClickListener;

    //提供set方法供Activity或Fragment调用
    public void setRecyclerItemClickListener(OnRecyclerItemClickOrderListener listener){
        monItemClickListener=listener;
    }

    public ManagerOrderRcvAdapter(Context context, List<Order> datas) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        orders = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = mInflater.inflate(R.layout.item_recycler_order_manager, parent, false);

        return new ItemViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if(holder instanceof ItemViewHolder){

            Order order = orders.get(position);
            Service service = order.getService();
            User older = order.getOlder();
            Worker worker = order.getWorker();

            ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            itemViewHolder.serName_tv.setText(service.getName());
            itemViewHolder.serTime_tv.setText(order.getService_starttime()+" 至 "+order.getService_endtime());
            if (older != null){
                itemViewHolder.olderName_tv.setText(older.getName());
                itemViewHolder.olderSex_tv.setText(older.getSex());
                itemViewHolder.olderAge_tv.setText(DateUtil.getAgeFromBirthTime(older.getBirth())+"岁");
                itemViewHolder.olderPhone_tv.setText(older.getPhone());
                itemViewHolder.olderAddress_tv.setText(older.getAdress());

                if(worker != null){
                    itemViewHolder.workerName_tv.setText(worker.getName());
                    itemViewHolder.workerSex_tv.setText(worker.getSex());
                    itemViewHolder.workerPhone_tv.setText(worker.getPhone());
                }

            }
        }
    }

    @Override
    public int getItemCount() {
        return orders.size();
    }

    @Override
    public int getItemViewType(int position) {

        return super.getItemViewType(position);
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.cardView)
        CardView cardView;
        @BindView(R.id.serName_tv)
        TextView serName_tv;
        @BindView(R.id.serTime_tv)
        TextView serTime_tv;
        @BindView(R.id.olderName_tv)
        TextView olderName_tv;
        @BindView(R.id.olderSex_tv)
        TextView olderSex_tv;
        @BindView(R.id.olderAge_tv)
        TextView olderAge_tv;
        @BindView(R.id.olderPhone_tv)
        TextView olderPhone_tv;
        @BindView(R.id.olderAddress_tv)
        TextView olderAddress_tv;
        @BindView(R.id.workerName_tv)
        TextView workerName_tv;
        @BindView(R.id.workerSex_tv)
        TextView workerSex_tv;
        @BindView(R.id.workerPhone_tv)
        TextView workerPhone_tv;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (monItemClickListener!=null){
                        monItemClickListener.onItemClick(getAdapterPosition(),orders);
                    }
                }
            });

        }

    }
}
